from muffin import Application, ResponseHTML

app = Application(static_url_prefix='/assets', static_folders=['static'])

@app.route('/', '/hello/{name}')
async def hello(request):
    name = request.path_params.get('name', 'world')
    return {
        'msg': name.title()
    }

@app.middleware
async def simple_md(app, request, receive, send):
    try:
        response = await app(request, receive, send)
        response.headers['x-simple-md'] = 'passed'
        return response
    except RuntimeError:
        return ResponseHTML('Middleware Exception')

subapp = app
@subapp.route('/hi')
def subpage(request):
    return 'OK from subapp'

# /sub/hi Connect the subapplication with an URL prefix
app.route('/sub')(subapp)

if __name__ == "__main__":

    # uvicorn main:app
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000, log_level="debug")
